package org.example.ExperimentTwo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StudentFileManager {

    public static void StudentData(String inputFile, String outputFile) {
//        读文件
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {

            String line;
//           格式读取
            Pattern pattern = Pattern.compile("Student\\(name:(.*?)\\|password:(.*?)\\)");
            while ((line = reader.readLine()) != null) {
//                格式matcher匹配
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    String name = matcher.group(1);
                    String originalPassword = matcher.group(2);

                    Student student = new Student(name, "123456"); // 设置密码我为123456
                    writer.write(student.toString());
                    writer.newLine();
                } else {
                    System.out.println("警告" + line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String inputFilePath = "E:\\Java\\JavaExperimentOne\\Student.txt"; // 输入文件路径
        String outputFilePath = "E:\\Java\\JavaExperimentOne\\students_updated.txt"; // 输出文件路径
        StudentData(inputFilePath, outputFilePath);
        System.out.println("数据更新成功.");
    }
}